﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JPGResizer.Pages
{
    /// <summary>
    /// Interaction logic for Page2.xaml
    /// </summary>
    public partial class Page2 : Page
    {
        public Page2()
        {
            InitializeComponent();
            Bind_Events();
        }

        private void Bind_Events()
        {
            this.Loaded += (o, e) =>
                {
                    Bind_Datas();
                };

            #region rbResizeType1
            this.rbResizeType1.Checked += (o, e) =>
                {
                    Helper.ValueHelper.CurrentValues.ResizeType = Helper.ResizeType.ResizeType1;
                };
            this.tbWith.TextChanged += (o, e) =>
                {
                    try
                    {
                        Helper.ValueHelper.CurrentValues.Length = Convert.ToInt32(this.tbWith.Text);
                    }
                    catch { }
                }; 
            #endregion

            #region ResizeType2
            this.rbResizeType2.Checked += (o, e) =>
                 {
                     Helper.ValueHelper.CurrentValues.ResizeType = Helper.ResizeType.ResizeType2;
                 };
            this.tbLength.TextChanged += (o, e) =>
                {
                    try
                    {
                        Helper.ValueHelper.CurrentValues.Length = Convert.ToInt32(this.tbLength.Text);
                    }
                    catch { }
                }; 
            #endregion

            #region ResizeType3
            this.rbResizeType3.Checked += (o, e) =>
                   {
                       Helper.ValueHelper.CurrentValues.ResizeType = Helper.ResizeType.ResizeType3;
                   };
            this.sliderResizePercent.ValueChanged += (o, e) =>
                {
                    if (this.tbResizePercent.Text.Trim() != this.sliderResizePercent.Value.ToString())
                    {
                        if (this.sliderResizePercent.Value > 0 && this.sliderResizePercent.Value <= 100)
                        {
                            Helper.ValueHelper.CurrentValues.ResizePercent = this.sliderResizePercent.Value;
                        }
                        Bind_Datas();
                    }
                };
            this.tbResizePercent.TextChanged += (o, e) =>
                {
                    if (this.tbResizePercent.Text.Trim() != this.sliderResizePercent.Value.ToString())
                    {
                        try
                        {
                            double value = Convert.ToDouble(this.tbResizePercent.Text.Trim());
                            if (value <= 100 && value > 0)
                            {
                                Helper.ValueHelper.CurrentValues.ResizePercent = value;
                            }
                            Bind_Datas();
                        }
                        catch { }
                    }
                };
            #endregion
        }

        private void Bind_Datas()
        {
            switch(Helper.ValueHelper.CurrentValues.ResizeType)
            {
                case Helper.ResizeType.ResizeType1:
                    {
                        this.rbResizeType1.IsChecked=true;
                        break;
                    }
                case Helper.ResizeType.ResizeType2:
                    {
                        this.rbResizeType2.IsChecked=true;
                        break;
                    }
                case Helper.ResizeType.ResizeType3:
                    {
                        this.rbResizeType3.IsChecked=true;
                        break;
                    }
                case Helper.ResizeType.ResizeType4:
                    {
                        this.rbResizeType4.IsChecked=true;
                        break;
                    }
            }
            this.tbWith.Text = Helper.ValueHelper.CurrentValues.Length.ToString();
            this.tbLength.Text = Helper.ValueHelper.CurrentValues.Length.ToString();
            this.sliderResizePercent.Value = Helper.ValueHelper.CurrentValues.ResizePercent;
            this.tbResizePercent.Text = Helper.ValueHelper.CurrentValues.ResizePercent.ToString();
        }
    }
}
